<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/9/18
 * Time: 11:52
 */

namespace app\manage\controller;
class Link extends Base
{
    private $mod;
    protected $beforeActionList = [
        'beforeIndex'=>['only'=>'index'],
        'beforeEdit' =>['only'=>'add,edit']
    ];
    public function _initialize() {
        parent::_initialize();
        $this->mod = model('link');
    }
    public function beforeIndex()
    {
        $big_menu = array(
            'title' => '添加链接',
            'iframe' => url('Link/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '210'
        );
        $this->_ajaxedit = true;
        $cate_list = model('link_cate')->where('status',1)->select();
        $this->assign('options',$this->check());
        $this->assign('cate_list',$cate_list);
        $this->assign('big_menu', $big_menu);
    }
    public function beforeEdit(){
        $cate_list = model('link_cate')->where('status',1)->select();
        $this->assign('cate_list',$cate_list);
    }
    public function _search(){
        $cate_id = input('param.cate_id/d');
        $keyword = input('param.keyword');
        $map = '';
        $keyword && $map['name'] = ['like','%'.$keyword.'%'];
        $cate_id && $map['cate_id'] = $cate_id;
        $search = [
          'cate_id' => $cate_id,
            'keyword' => $keyword
        ];
        $this->assign('search',$search);
        return $map;
    }
    public function delete(){
        \app\common\model\LinkCate::event('before_delete',function($obj){
            //分类下存在链接不能删除
            $id = $obj->id;
            $manager = model('link')->where(['cate_id'=>$id])->count('id');
            if($manager > 0){
                return false;
            }
            return true;
        });

        parent::delete();
    }

}